<?php
  require_once 'DB.php';
  include_once("./libraries/db.inc.php");
  include_once("./libraries/authentication.inc.php");
  include_once("./queries/jobQueue.inc.php");
  include_once("./queries/jobHistory.inc.php");
  include_once("./queries/machine.inc.php");
  include_once("./libraries/util.inc.php");
  include_once("./libraries/displayUtils.inc.php");

  session_start();
  sessionAuthenticate("login.php");

  $connection = DB::connect($dsn, $options);
  if (DB::isError($connection)) {
    trigger_error($connection->getMessage(), E_USER_ERROR);
  }

  printHeader("User Interface");
  cdbBody();
  cdbSetUp();
  echo "<font size=+3><b><center>",appName," User Interface" .
       "</center></b></font><br />"
?>
<!-- ***** BODY GOES FROM HERE UNTIL NEXT COMMENT WITH *'S ***** -->

<!-- for now let's put the job queue info here -->
<?php
  //phpinfo();
  //print_r($_SESSION["loginUsername"]);
  //echo "Administrator = ";
  //print_r($_SESSION["administrator"]);
  if ($_SESSION["administrator"] === true) {
	  printLink("adminIndex.php", "Take me to the Admin screen!<br /><br />");
  } 
  //echo "You are not an administrator";
  
  $wait = count_user_jobs_by_status($connection, array("waiting"));
  $hold = count_user_jobs_by_status($connection, array("held"));
  $run = count_user_jobs_by_status($connection, array("running"));
  $comp = count_user_jobs_by_status($connection, array("completed"));
 
  echo "<table width=100%>\n";

  echo "<tr>\n";
  echo "<td><a href=jobQueueList.php?refresh=true&waiting=true&owner={$_SESSION["loginUsername"]}>Your jobs waiting in the queue:</a> {$wait}</td>\n";
  if ($wait + $hold + $run > 0) {
	  echo "<td width=50% rowspan=3 align=right>\n";
	  echo "<img src=indexGraph.php?wait={$wait}&hold={$hold}&run={$run}&dummy=" . time() . ">";
	  echo "</td>\n";
  }
  echo "</tr>\n";

  echo "<tr>\n";
  echo "<td> <a href=jobQueueList.php?refresh=true&held=true&owner={$_SESSION["loginUsername"]}>Your jobs on hold:</a> {$hold}</td>\n";
  echo "</tr>\n";

  echo "<tr>\n";
  echo "<td> <a href=jobQueueList.php?refresh=true&running=true&owner={$_SESSION["loginUsername"]}>Your jobs currently executing:</a> {$run}</td>\n";
  echo "</tr>\n";

  echo "<tr>\n";
  echo "<td> <a href=jobQueueList.php?refresh=true&completed=true&owner={$_SESSION["loginUsername"]}>Your jobs completed in the queue :</a> {$comp}</td>\n";
  echo "</tr>\n";

  echo "<tr>\n";
	  //echo "$interval";
  echo "<td> <a href=jobHistoryList.php?refresh=true&interval_scalar=24&interval_unit=hour&owner={$_SESSION["loginUsername"]}>Your jobs run in last 24 hours:</a> " . count_user_job_history_by_status($connection, array(), 24, "hour") . "</td>\n";
  echo "</tr>\n";

  echo "<tr><td> <a href=jobHistoryList.php?refresh=true&interval_scalar=none&interval_unit=none&owner={$_SESSION["loginUsername"]}>All your jobs that have ever run:</a> " . count_user_job_history_by_status($connection, array(), "none", "none") . "</td></tr>\n";

  echo "</table>\n";

//  printLink("provenance.php", "<br />Take me to the provenance screen.");



/*
  echo "<p>These admin type queries will get moved somewhere else<p>\n";
  echo "<table width=100%>\n";
  echo "<tr><td> <a href=machineReport.php>Machines in the pool right now:</a> " . machine_pool_count($connection) . "</td></tr>\n";
  echo "<tr><td> <a href=machineList.php?before=5%20minutes&after=1%20hour>Machines lost in the last hour:</a> " . countLostMachines($connection, "5 minutes", "1 hour") . "</td></tr>\n";
  echo "<tr><td> <a href=machineList.php?before=1%20hour&after=1%20day>Machines lost in the last day:</a> " . countLostMachines($connection, "1 hour", "1 day") . "</td></tr>\n";
  echo "<tr><td> <a href=machineList.php?before=1%20day&after=1%20week>Machines lost in the last week:</a> " . countLostMachines($connection, "1 day", "1 week") . "</td></tr>\n";
  echo "<tr><td> <a href=machineList.php?before=1%20week&after=1%20month>Machines lost in the last month:</a> " . countLostMachines($connection, "1 week", "1 month") . "</td></tr>\n";
  echo "<tr><td> <a href=machineList.php?before=1%20month>Machines lost more than 1 month ago:</a> " . countLostMachines($connection, "1 month", null) . "</td></tr>\n";
  echo "</table>";
*/

?>


<!-- ***** END OF BODY ***** -->
<?php
  cdbFinishPage();
  $connection->disconnect();
?>
